package stack.simple;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * 最小栈
 * 示例:
 *
 * 输入：
 * ["MinStack","push","push","push","getMin","pop","top","getMin"]
 * [[],[-2],[0],[-3],[],[],[],[]]
 *
 * 输出：
 * [null,null,null,null,-3,null,0,-2]
 *
 * 解释：
 * MinStack minStack = new MinStack();
 * minStack.push(-2);
 * minStack.push(0);
 * minStack.push(-3);
 * minStack.getMin();   --> 返回 -3.
 * minStack.pop();
 * minStack.top();      --> 返回 0.
 * minStack.getMin();   --> 返回 -2.
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/min-stack
 */
public class MinStack_155 {

    Stack<Integer> data;
    public MinStack_155() {
        data = new Stack<>();
    }

    public void push(int val) {
       if(data.isEmpty()) {
           data.push(val);
           data.push(val);
       } else {
           int min = Math.min(data.peek(), val);
           data.push(val);
           data.push(min);
       }
    }

    public void pop() {
        data.pop();
        data.pop();
    }

    public int top() {
        Integer min = data.pop();

        int top = data.peek();
        data.push(min);
        return top;
    }

    public int getMin() {
        return data.peek();
    }

    public static void main(String[] args) {

    }
}
